package edu.sandau.basic.controller;

import edu.sandau.basic.service.RoleMenuService;
import edu.sandau.basic.vo.BasicMsg;
import edu.sandau.basic.vo.RoleMenuVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
@Tag(name = "角色菜单管理", description = "角色与菜单权限的增删改查接口")
public class RoleMenuController {

    @Autowired
    private RoleMenuService roleMenuService;

    @Operation(summary = "查询角色绑定的菜单ID")
    @GetMapping("/role_menus/{roleId}")
    public List<Integer> getRoleMenus(@PathVariable Integer roleId) {
        return roleMenuService.getMenuIdsByRole(roleId);
    }

    @Operation(summary = "更新角色菜单权限（覆盖旧绑定）")
    @PutMapping("/role_menus/{roleId}")
    public BasicMsg updateRoleMenus(@PathVariable Integer roleId,
                                    @RequestBody RoleMenuVo vo) {
        roleMenuService.updateRoleMenus(roleId, vo.getMenuIds());
        return BasicMsg.ok("角色菜单权限更新成功");
    }

    @Operation(summary = "删除角色的所有菜单绑定")
    @DeleteMapping("/role_menus/{roleId}")
    public BasicMsg deleteRoleMenus(@PathVariable Integer roleId) {
        roleMenuService.deleteRoleMenus(roleId);
        return BasicMsg.ok("已清空角色的所有菜单绑定");
    }



}
